package com.martin.lib_base.ui.screen

import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowRight
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.PathEffect
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import coil.compose.AsyncImage
import com.martin.lib_base.R
import com.martin.lib_base.ui.theme.ColorTextLightGray
import dev.DevUtils

/**
 * @author：孟凡华
 * @date：2023/7/13
 * @desc：
 */

@Composable
fun Center(modifier: Modifier = Modifier, content: @Composable BoxScope.() -> Unit) {
    Box(
        contentAlignment = Alignment.Center,
        modifier = modifier,
        content = content
    )
}

@Composable
fun CenterColumn(modifier: Modifier = Modifier, content: @Composable ColumnScope.() -> Unit) {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
        modifier = modifier,
        content = content
    )
}

@Composable
fun CenterRow(modifier: Modifier = Modifier, content: @Composable RowScope.() -> Unit) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Center,
        modifier = modifier,
        content = content
    )
}

@Composable
fun CircleImage(
    model: Any?,
    size: Dp,
    modifier: Modifier = Modifier
) {
    AsyncImage(
        model = model,
        contentDescription = null,
        contentScale = ContentScale.Crop,
        modifier = modifier
            .then(
                Modifier
                    .size(size)
                    .clip(CircleShape)
            )
    )
}


@Composable
fun FillSizeImage(
    model: Any?,
    contentScale: ContentScale = ContentScale.FillBounds,
    modifier: Modifier = Modifier
) {
    val realModel = remember {
        val modelInt = model?.toString()?.toIntOrNull()
        if (modelInt != null && modelInt != 0) ContextCompat.getDrawable(
            DevUtils.getTopActivity(),
            modelInt
        ) else model
    }
    AsyncImage(
        model = realModel,
        contentDescription = null,
        contentScale = contentScale,
        modifier = Modifier
            .fillMaxSize()
            .then(modifier)
    )
}

@Composable
fun CommonItemRightIcon(size: Dp = 15.dp) {
    Icon(
        imageVector = Icons.Filled.KeyboardArrowRight,
        contentDescription = null,
        tint = ColorTextLightGray,
        modifier = Modifier
            .padding(start = 5.dp)
            .size(size)
    )
}

/**
 * 正方形
 */
@Composable
fun SquareBox(content: @Composable BoxScope.() -> Unit) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .aspectRatio(1f),
        content = content
    )
}

@Composable
fun CommonCheckImage(
    checked: Boolean,
    size: Dp = 15.dp,
    modifier: Modifier = Modifier,
) {
    AsyncImage(
        model = if (checked) R.drawable.img_check_s else R.drawable.img_check_n,
        contentDescription = null,
        modifier = modifier
            .size(size)
    )
}

/**
 * 虚线
 */
@Composable
fun DottedLine(
    color: Color = Color.LightGray,
    modifier: Modifier = Modifier
        .fillMaxWidth()
        .height(1.dp)
) {
    Canvas(
        modifier = modifier
    ) {
        val pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f), 0f)
        drawLine(
            color = color,
            start = Offset(0f, 0f),
            end = Offset(size.width, 0f),
            pathEffect = pathEffect
        )
    }
}
